﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.Web.Mvc;
using RazorBridge.Mvc;

namespace RazorBridge
{
    /// <summary>
    /// WebFramework contains customization for MVC Project
    /// </summary>
   public static class RazorFramework
    {
       /// <summary>
       /// Initialize the Application
       /// Register Virtual Path Provider and CompositeViewEngine for Razor
       /// </summary>
       public static void AppInitialize()
       {
           HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider());
           RegisterCompositeViewEngine();
       }

       /// <summary>
       /// Remove WebFormViewEngine and add custom RazorView Engine
       /// </summary>
       private static void RegisterCompositeViewEngine()
       {
           var razorViewEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
           if (razorViewEngine != null)
           {
               ViewEngines.Engines.Remove(razorViewEngine);    
           }
           ViewEngines.Engines.Add(new CompositeRazorEngine());
           var webFormViewEngine = ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault();
           if (webFormViewEngine != null)
           {
               ViewEngines.Engines.Remove(webFormViewEngine);               
           }
       }
    }
}
